home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Draw / Sources / GroupCmd.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  11.8 KB  |  341 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                GroupCmd.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef GROUPCMD_H
  15. #include "GroupCmd.h"
  16. #endif
  17.  
  18. #ifndef DRAWCONT_H
  19. #include "DrawCont.h"
  20. #endif
  21.  
  22. #ifndef DRAWPART_H
  23. #include "DrawPart.h"
  24. #endif
  25.  
  26. #ifndef DRAWSEL_H
  27. #include "DrawSel.h"
  28. #endif
  29.  
  30. #ifndef BASESHP_H
  31. #include "BaseShp.h"
  32. #endif
  33.  
  34. #ifndef GROUPSHP_H
  35. #include "GroupShp.h"
  36. #endif
  37.  
  38. // for cGroup
  39. #ifndef DEFINES_K
  40. #include "Defines.k"
  41. #endif
  42.  
  43. // ----- FrameWork Includes -----
  44.  
  45. #ifndef FWPRESEN_H
  46. #include "FWPresen.h"
  47. #endif
  48.  
  49. #ifndef SOM_ODShape_xh
  50. #include <Shape.xh>
  51. #endif
  52.  
  53. //========================================================================================
  54. // RunTime Info
  55. //========================================================================================
  56.  
  57. #ifdef FW_BUILD_MAC
  58. #pragma segment odfdrawcommand
  59. #endif
  60.  
  61. FW_DEFINE_AUTO(CGroupContent)
  62. FW_DEFINE_AUTO(CGroupShapesCommand)
  63. FW_DEFINE_AUTO(CUngroupShapesCommand)
  64.     
  65. //========================================================================================
  66. // class CGroupContent
  67. //========================================================================================
  68.  
  69. //----------------------------------------------------------------------------------------
  70. //    CGroupContent constructor
  71. //----------------------------------------------------------------------------------------
  72. CGroupContent::CGroupContent(Environment* ev, CDrawPart* drawPart, CDrawContent* other)
  73. :    CDrawContent(ev, drawPart, other)
  74. {
  75.     FW_END_CONSTRUCTOR    
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. //    CGroupContent destructor
  80. //----------------------------------------------------------------------------------------
  81. CGroupContent::~CGroupContent()
  82. {
  83.     FW_START_DESTRUCTOR
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. //    CGroupContent::GetShapeList
  88. //----------------------------------------------------------------------------------------
  89. CShapeCollection* CGroupContent::GetShapeList()
  90. {
  91.     return fShapeList;
  92. }
  93.  
  94. //========================================================================================
  95. // CGroupShapesCommand
  96. //========================================================================================
  97.  
  98. //----------------------------------------------------------------------------------------
  99. //    CGroupShapesCommand constructor
  100. //----------------------------------------------------------------------------------------
  101. CGroupShapesCommand::CGroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
  102. :    FW_CCommand(ev, cGroup, frame, FW_kCanUndo),
  103.     fDrawSelection(selection),
  104.     fGroupShape(NULL),
  105.     fGroupedContent(NULL)
  106. {
  107.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoGroupMsg, kRedoGroupMsg);
  108.     FW_END_CONSTRUCTOR    
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. //    CGroupShapesCommand destructor
  113. //----------------------------------------------------------------------------------------
  114. CGroupShapesCommand::~CGroupShapesCommand()
  115. {
  116.     FW_START_DESTRUCTOR
  117.     delete fGroupedContent;
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. //    CGroupShapesCommand::FreeRedoState
  122. //----------------------------------------------------------------------------------------
  123. void CGroupShapesCommand::FreeRedoState(Environment* ev)
  124. {
  125. FW_UNUSED(ev);
  126.     // Delete the group shape, but not the shapes it contains
  127.     fGroupShape->EmptyShapes(false);    // remove shapes, but don't delete them
  128.     delete fGroupShape;                    // deletes the shapeList
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. //    CGroupShapesCommand::DoIt
  133. //----------------------------------------------------------------------------------------
  134. void CGroupShapesCommand::DoIt(Environment* ev)
  135. {
  136.     // Save Undo state - copy selected shapes
  137.     fGroupedContent = FW_NEW(CGroupContent, (ev, fDrawSelection->GetDrawPart(), fDrawSelection->GetDrawContent(ev)));
  138.  
  139.     // Combine selected shapes into a new group shape
  140.     CShapeCollection* shapeList = fGroupedContent->GetShapeList();
  141.     fGroupShape = FW_NEW(CGroupShape, ((CDrawPart*) fDrawSelection->GetPart(ev), shapeList));
  142.  
  143.     this->GroupShapes(ev);
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. //    CGroupShapesCommand::UndoIt
  148. //----------------------------------------------------------------------------------------
  149. void CGroupShapesCommand::UndoIt(Environment* ev)
  150. {
  151.     // Ungroup the shapes
  152.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  153.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  154.  
  155.     // Add each shape back into the part's list
  156.     CDrawContentShapeIterator it(fGroupedContent);
  157.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  158.     {
  159.         partContent->AddShape(ev, shape, fGroupShape);    // insert before the group shape
  160.     }
  161.  
  162.     // Remove group shape from the part
  163.     partContent->RemoveShape(ev, fGroupShape);
  164.  
  165.     // Select the shapes
  166.     fDrawSelection->SelectContent(ev, fGroupedContent);
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. //    CGroupShapesCommand::RedoIt
  171. //----------------------------------------------------------------------------------------
  172. void CGroupShapesCommand::RedoIt(Environment* ev)
  173. {
  174.     this->GroupShapes(ev);
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. //    CGroupShapesCommand::GroupShapes
  179. //----------------------------------------------------------------------------------------
  180. void CGroupShapesCommand::GroupShapes(Environment* ev)
  181. {
  182.     // Remove shapes from the selection
  183.     fDrawSelection->CloseSelection(ev);
  184.  
  185.     // Remove each shape from the part's list (but don't detach it)
  186.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  187.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  188.     
  189.     CDrawContentShapeIterator it(fGroupedContent);
  190.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  191.     {
  192.         partContent->RemoveShape(ev, shape);
  193.     }
  194.  
  195.     // Add the new group shape to the part and select it
  196.     drawPart->AddShapeToPart(ev, fGroupShape);
  197.     fDrawSelection->AddToSelection(ev, fGroupShape, true);
  198. }
  199.  
  200. //========================================================================================
  201. // CUngroupShapesCommand
  202. //========================================================================================
  203.  
  204. //----------------------------------------------------------------------------------------
  205. //    CUngroupShapesCommand constructor
  206. //----------------------------------------------------------------------------------------
  207. CUngroupShapesCommand::CUngroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
  208. :    FW_CCommand(ev, cUngroup, frame, FW_kCanUndo),
  209.     fDrawSelection(selection),
  210.     fGroupList(NULL)
  211. {
  212.     // Copy selected shapes that are group shapes
  213.     fGroupList = FW_NEW(CShapeCollection, ());
  214.     CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
  215.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  216.     {
  217.         if (shape->GetShapeType() == kGroupShape)
  218.             fGroupList->AddLast(shape);
  219.     }
  220.  
  221.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoUngroupMsg, kRedoUngroupMsg);
  222.     FW_END_CONSTRUCTOR    
  223. }
  224.  
  225. //----------------------------------------------------------------------------------------
  226. //    CUngroupShapesCommand destructor
  227. //----------------------------------------------------------------------------------------
  228. CUngroupShapesCommand::~CUngroupShapesCommand()
  229. {
  230.     FW_START_DESTRUCTOR
  231.     delete fGroupList;
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. //    CUngroupShapesCommand::FreeUndoState
  236. //----------------------------------------------------------------------------------------
  237. void CUngroupShapesCommand::FreeUndoState(Environment* ev)
  238. {
  239. FW_UNUSED(ev);
  240.     // Delete the saved group shapes, since they're obsolete
  241.     CBaseShape* shape;
  242.     while ((shape = fGroupList->First()) != NULL)
  243.     {
  244.         fGroupList->Remove(shape);
  245.         ((CGroupShape*)shape)->EmptyShapes(false);    // don't delete shapes
  246.         delete shape;
  247.     }
  248. }
  249.  
  250. //----------------------------------------------------------------------------------------
  251. //    CUngroupShapesCommand::DoIt
  252. //----------------------------------------------------------------------------------------
  253. void CUngroupShapesCommand::DoIt(Environment* ev)    // Override
  254. {
  255.     if (fGroupList->Count() == 0) return;    // nothing to do
  256.  
  257.     // Remove shapes from the selection
  258.     fDrawSelection->CloseSelection(ev);
  259.  
  260.     // Iterate saved shapes and ungroup each one
  261.     CShapeCollectionIterator it(fGroupList);
  262.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  263.     {
  264.         this->UngroupShape(ev, (CGroupShape*)shape);
  265.     }
  266.  
  267.     fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  268. }
  269.  
  270. //----------------------------------------------------------------------------------------
  271. //    CUngroupShapesCommand::UndoIt
  272. //----------------------------------------------------------------------------------------
  273. void CUngroupShapesCommand::UndoIt(Environment* ev)    // Override
  274. {
  275.     if (fGroupList->Count() == 0) return;    // nothing to do
  276.  
  277.     // Remove shapes from the selection
  278.     fDrawSelection->CloseSelection(ev);
  279.  
  280.     // Iterate saved shapes and regroup each one
  281.     CShapeCollectionIterator it(fGroupList);
  282.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  283.     {
  284.         this->RegroupShape(ev, (CGroupShape*)shape);
  285.     }
  286.  
  287.     fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  288. }
  289.  
  290. //----------------------------------------------------------------------------------------
  291. //    CUngroupShapesCommand::RedoIt
  292. //----------------------------------------------------------------------------------------
  293. void CUngroupShapesCommand::RedoIt(Environment* ev)    // Override
  294. {
  295.     this->DoIt(ev);
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //    CUngroupShapesCommand::UngroupShape
  300. //----------------------------------------------------------------------------------------
  301. void CUngroupShapesCommand::UngroupShape(Environment* ev, CGroupShape* groupShape)
  302. {
  303.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  304.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  305.  
  306.     // Remove the group shape itself from the part
  307.     CBaseShape* followingShape = partContent->GetShapeAfter(groupShape);
  308.     partContent->RemoveShape(ev, groupShape);
  309.  
  310.     // Iterate the shapes in the group and add each to the part, in the right position
  311.     CShapeCollectionIterator it(groupShape->GetShapeList());
  312.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  313.     {
  314.         partContent->AddShape(ev, shape, followingShape);
  315.         fDrawSelection->AddToSelection(ev, shape, false);
  316.     }
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. //    CUngroupShapesCommand::RegroupShape
  321. //----------------------------------------------------------------------------------------
  322. void CUngroupShapesCommand::RegroupShape(Environment* ev, CGroupShape* groupShape)
  323. {
  324.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  325.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  326.  
  327.     // Remove the shapes from the part
  328.     CBaseShape* followingShape = NULL;
  329.     CShapeCollectionIterator it(groupShape->GetShapeList());
  330.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  331.     {
  332.         followingShape = partContent->GetShapeAfter(shape);
  333.         partContent->RemoveShape(ev, shape);
  334.     }
  335.  
  336.     // Add the group shape back into the part, in the right position
  337.     partContent->AddShape(ev, groupShape, followingShape);
  338.     fDrawSelection->AddToSelection(ev, groupShape, false);
  339. }
  340.  
  341.